package com.sdp.th.netty.listener;

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.th.netty.server.NettyServer;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class ApplicationEventListener  implements ApplicationListener<ApplicationEvent>{

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof ApplicationEnvironmentPreparedEvent) {
			log.info("********初始化环境变量");
		}else if (event instanceof ApplicationPreparedEvent) { // 初始化完成
			log.info("********初始化环境变量完成");
		}else if (event instanceof ContextRefreshedEvent) {
			log.info("********应用刷新");
		}else if (event instanceof ApplicationReadyEvent) {
			log.info("********应用已启动完成");
			NettyServer nettyServer = ApplicationContextUtils.getBean(NettyServer.class);
			try {
				if(nettyServer.start()) {
					log.info("********netty启动成功");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
			
		
	}

}
